package shangji;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Shangji1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        // 工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        // 解析文档
        Document doc = db.parse("src/Shangji/Shangji1.xml");
        // 读取Brand
        NodeList brandlist = doc.getElementsByTagName("Brand");
        NodeList typelist = doc.getElementsByTagName("Type");
        for (int i=0;i<brandlist.getLength();i++){
            Node brand = brandlist.item(i);
            Element eleBrand = (Element) brand;
            String eleBrandName = eleBrand.getAttribute("name");
            System.out.println("手机品牌为:"+eleBrandName);
        }
        for (int j=0;j< typelist.getLength();j++){
            Node type = typelist.item(j);
            Element eleType = (Element) type;
            String eleTypeName = eleType.getAttribute("name");
            System.out.println("手机类型为:"+eleTypeName);
        }
    }
}
